//
//  GHPhotoBrowserAnimator.swift
//  Browser
//
//  Created by 刘光华 on 16/4/28.
//  Copyright © 2016年 liuguanghua. All rights reserved.
//

import UIKit
import Masonry
class GHPhotoBrowserAnimator: NSObject {

    var isPresenting: Bool = false
    var indexPath: NSIndexPath?
    
    weak var presentDelegate: GHPresentDelegate?
    weak var dismissDelegate: GHDismissDelegate?
    
}

// MARK:- 弹出动画协议
// 在这个协议中可以获取弹出动画的控件
protocol GHPresentDelegate: class {
    
    func imageView(indexPath: NSIndexPath)-> UIImageView
    func imageViewHomePosition(indexPath: NSIndexPath)-> CGRect
    func imageViewFinalPosition(indexPath: NSIndexPath)-> CGRect
    
}

// MARK:- 消失动画协议

protocol GHDismissDelegate: class {
    
    func currentIndexPath()-> NSIndexPath
    func imageView()-> UIImageView
    func imageViewHomePosition(indexPath: NSIndexPath)-> CGRect
//    func imageViewFinalPosition(indexPath: NSIndexPath)-> CGRect
    
}

extension GHPhotoBrowserAnimator: UIViewControllerTransitioningDelegate {
    
    /// 消失动画交给谁来管理
    func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresenting = false
        return self
    }
    
    /// 弹出动画交给谁来管理
    func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresenting = true
        return self
    }
    
   
}

extension GHPhotoBrowserAnimator: UIViewControllerAnimatedTransitioning {
    
    /// 1. 返回动画执行时间
    func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
        return 0.9
    }
    
    /// 2. 可以获取转场的上下文:可以通过上下文获取到执行动画的View
    // UITransitionContextFromViewKey : 取出是消失的View,是在做消失动画时会使用
    // UITransitionContextToViewKey : 取出是弹出的View,是在做弹出动画时会使用
    func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
        
        isPresenting ? animateForPresentView(transitionContext) : animateForDismissView(transitionContext)
        
    }
    
    /// 弹出动画
    func animateForPresentView(transitionContext: UIViewControllerContextTransitioning) {
        // 1.取出弹出的View(控制器的View)
        let presentView = transitionContext.viewForKey(UITransitionContextToViewKey)!
        
        // 2.将弹出的View自己加到containerView中
        transitionContext.containerView()?.addSubview(presentView)
        
        // 3. 获取做动画的imageView,添加进控制器的view
        guard let presentDelegate = presentDelegate else {
            transitionContext.completeTransition(true)
            return
        }
        guard let indexPath = indexPath else {
            transitionContext.completeTransition(true)
            return
        }
        let imageView = presentDelegate.imageView(indexPath)
        transitionContext.containerView()?.addSubview(imageView)
        
        
        // 4. 获取起点和终点的frame
        let imageViewHomePosition = presentDelegate.imageViewHomePosition(indexPath)
        let imageViewFinalPosition = presentDelegate.imageViewFinalPosition(indexPath)
        
        
        // 5. 动画起始设置
        presentView.alpha = 0
        imageView.gh_addConstraintsWithFrame(imageViewHomePosition)
        transitionContext.containerView()?.layoutIfNeeded()
        
        
        // 6. 结束动画设置
        // 改变约束
        imageView.gh_remakeConstraintsWithFrame(imageViewFinalPosition)
        
        
        
        // update constraints now so we can animate the change
        presentView.updateConstraintsIfNeeded()
        
        // 执行动画
        UIView.animateWithDuration(transitionDuration(transitionContext), animations: { () -> Void in
            
            transitionContext.containerView()?.layoutIfNeeded()
            
            }) { (_) -> Void in
                presentView.alpha = 1
                imageView.removeFromSuperview()
                transitionContext.completeTransition(true)
        }
    }
    
    /// 消失动画
    func animateForDismissView(transitionContext: UIViewControllerContextTransitioning) {
        // 1.取出消失的View(控制器的View)
        let dismissView = transitionContext.viewForKey(UITransitionContextFromViewKey)!
        dismissView.removeFromSuperview()
        
        // 2. 空值校验
        guard let dismissDelegate = dismissDelegate else {
            return
        }
        guard let presentDelegate = presentDelegate else {
            return
        }
        let imageView = dismissDelegate.imageView()
        transitionContext.containerView()?.addSubview(imageView)
        
        
        // 3 获取当前正在显示的indexPath
        let indexPath = dismissDelegate.currentIndexPath()
        
        // 起始位置
        imageView.gh_addConstraintsWithFrame(dismissDelegate.imageViewHomePosition(indexPath))
        transitionContext.containerView()?.layoutIfNeeded()
        
        // 终止位置
        imageView.gh_remakeConstraintsWithFrame(presentDelegate.imageViewHomePosition(indexPath))
        
        
        // update constraints now so we can animate the change
        transitionContext.containerView()?.updateConstraintsIfNeeded()
        // 4.执行动画
        UIView.animateWithDuration(transitionDuration(transitionContext), animations: { () -> Void in
            
            transitionContext.containerView()?.layoutIfNeeded()
            
            }) { (_) -> Void in
                transitionContext.completeTransition(true)
        }

    }
    
    
    
}

